package ics314.ui.panel;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.io.File;
import java.io.IOException;

import ics314.Vor;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class SimpleVor extends JPanel{
	
	private Vor v;
	
	private Image ring;
	private Image internal;
	private Image pin;
	private Image to;
	private Image from;
	private Image sigGood;
	private Image sigBad;
	
	private static final int PIN_OFFSET_X = 160;
	private static final int PIN_OFFSET_Y = -70;
	private static final double PIN_FACTOR = 3.75;
	
	private static final int TO_X_OFFSET = 215;
	private static final int TO_Y_OFFSET = 132;
	
	private static final int FROM_X_OFFSET = 215;
	private static final int FROM_Y_OFFSET = 169;
	
	private static final int SIG_X_OFFSET = 137;
	private static final int SIG_Y_OFFSET = 235;

	public SimpleVor(Vor v) throws IOException {
		
		this.v = v;
		ring = ImageIO.read(new File("resources/img/VOR_ring_large.png"));
		internal = ImageIO.read(new File("resources/img/VOR_internal_large.png"));
		pin = ImageIO.read(new File("resources/img/VOR_pin_large.png"));
		to = ImageIO.read(new File("resources/img/VOR_to.png"));
		from = ImageIO.read(new File("resources/img/VOR_from.png"));
		sigGood = ImageIO.read(new File("resources/img/VOR_sig_good.png"));
		sigBad = ImageIO.read(new File("resources/img/VOR_sig_bad.png"));
		
		
	}
	
	public void paintComponent(Graphics g) {
			Graphics2D g2 = (Graphics2D) g;
			g2.drawImage(internal, 0, 0, this);
			double theta = Math.toRadians(v.getObs());
			
			g2.rotate(-theta, ring.getWidth(this) / 2, ring.getHeight(this) / 2);
			g2.drawImage(ring, 0, 0, this);
			
			g2.setTransform(new AffineTransform());
			
			if(v.isGoingTo()) {
				g2.drawImage(to, TO_X_OFFSET, TO_Y_OFFSET, this);
			}
			else {
				g2.drawImage(from, FROM_X_OFFSET, FROM_Y_OFFSET, this);
			}

			g2.setTransform(new AffineTransform());
			
			g2.translate(PIN_OFFSET_X, PIN_OFFSET_Y);
			g2.rotate(-Math.toRadians(v.getDegreesOff() * PIN_FACTOR), pin.getWidth(this) / 2, pin.getHeight(this) / 2);
			
			g2.drawImage(pin, 0, 0, this);
			
			g2.setTransform(new AffineTransform());
			
			if(v.isGood()) {
				g2.drawImage(sigGood, SIG_X_OFFSET, SIG_Y_OFFSET, this);
			}
			else {
				g2.drawImage(sigBad, SIG_X_OFFSET, SIG_Y_OFFSET, this);
			}
			
			g2.drawString("RAD: " + v.getRadial() + ", OBS: " + v.getObs() + ", TO: " + (v.isGoingTo() ? "T" : "F") +  ", SIG: " + (v.isGood() ? "T" : "F") + ", DEF: " + v.getDegreesOff(), 0, ring.getHeight(this) + 20);
			
	}
	
	
	
}
